import { findAll } from "@/http/batch";
import { getUser } from "@/http/login";
import { defineStore } from "pinia";

export default defineStore("userStore",{
    state() {
        return {
            checkBatch: [],
            checkUser:{
                account: "",
                maxArticle: 10000,
                maxBatch: 0,
            },
            Load: null
        }
    },
    getters: {
        batch() {
            return this.checkBatch
        },
        user() {
            return this.checkUser
        },
        getAll() {
            if (this.Load) return this.Load
            let arr = [this.getUser(),this.getBatch()]
            this.Load = Promise.all(arr)
            return this.Load
        }
    },
    actions:{
        setBatch(value){
            if(value instanceof Array)this.checkBatch.splice(0,this.checkBatch.length,...value)
        },
        getBatch() {
            return findAll().then(res=>{
                this.setBatch(res)
            }) 
        },
        setUser(value){
            if (!!value) {
                for(const key in this.checkUser){
                    this.checkUser[key] = value[key]
                }
            }
        },
        getUser() {
            return getUser().then(res=>{
                this.setUser(res)
            }) 
        },
        // getAll() {
        //     let arr = [this.getUser(),this.getBatch()]
        //     return Promise.all(arr)
        // }
    }
})